package com.itbour.web.action.yjx.trade;


import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.aliyun.openservices.shade.io.netty.util.internal.StringUtil;
import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.stats.UserDocInfoDto;
import com.itbour.dto.tg.TypeInfoDto;
import com.itbour.service.yjx.trade.YjxUserDocStatsService;
import com.opensymphony.xwork2.ActionContext;



/**
 * 		一键秀 模板使用统计情况
 */
public class GetYjxUserDocStatesAction extends BaseAction{
	
	
	private String isCount;
	private String findPubFlg;// 检索条件：发布标记
	private String findDocName;//检索的名称
	private String findTmplType;// 检索条件--分类计算的 
	private String findTmplTypetop;//顶级
	private String findDateStart;// 检索条件[时间范围(起始)
	private String findDateEnd;// 检索条件[时间范围(截止)
	private List<TypeInfoDto> typeComboList; //展示的信息 提纲信息
	private static final long serialVersionUID = 1L;
	
	
	private YjxUserDocStatsService yjxUserDocStatsService;
	HashMap<String, Object> mapResult = new HashMap<String, Object>();// 检索查询出来的结果
	HashMap<String, Object> userDocMapResult = new HashMap<String, Object>();// 检索查询出来的结果
	private UserDocInfoDto allUserDocInfo;
	private String findUniTmplId;// 统一模板ID
	private String findUserId;// 用户Id
	
	public String getMapInfoList() throws Exception{
		// 用户信息
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			//获取sort的参数排序
			String sort=request.getParameter("sort");
			String order=request.getParameter("order");
			Map<String, Object> param = new HashMap<String, Object>();
			order = (org.apache.commons.lang3.StringUtils.defaultString(order).length()==0)?"desc":order;
			sort = (org.apache.commons.lang3.StringUtils.defaultString(sort).length()==0)?"relAplyCnt":sort;
			StringBuffer ordersb = new StringBuffer(); 
			ordersb.append(sort).append(" ").append(order);
			param.put("order_num", ordersb.toString());//添加参数
			SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			SimpleDateFormat sim2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			if (StringUtils.isNotBlank(findTmplType) && Long.valueOf(findTmplType)>0L) {
				param.put("type_id", findTmplType);
			}
			if (StringUtils.isNotBlank(findTmplTypetop) && Long.valueOf(findTmplTypetop)>0L) {
				param.put("top_type", findTmplTypetop);// 顶级的父类来查询的条件 
			}
			if (StringUtils.isNotBlank(findDocName)) {// 添加
				param.put("design_name", findDocName);
			}
			if (StringUtils.isNotBlank(findDateStart)) {
				param.put("crtDateStart", sim2.format(sim.parse(findDateStart)));
			}
			if (StringUtils.isNotBlank(findDateEnd)) {
				param.put("crtDateEnd", sim2.format(sim.parse(findDateEnd)));
			}
			if (StringUtils.isNotBlank(findPubFlg)) {
				param.put("publish", findPubFlg);
			}
			// 默认只检索自己的文档([管理员,模版操作员]除外)
			if(!user.isAdmin() && (user.getRole()&32)!=32) {
				param.put("user_id", user.getUser_id());
			} else {
				if (StringUtils.isNotEmpty(findUserId)){
					param.put("user_id", findUserId);
				}
			}
			
			
			if (StringUtils.isNotEmpty(findUniTmplId)){// 统一模板ID
				param.put("uni_item_id",findUniTmplId);
			}
			//翻页查询
			Page page = yjxUserDocStatsService.pageQueryStats(param, Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			HashMap<String, Object> resMap = new HashMap<String, Object>();
			resMap.put("total", page.getTotalCount());
			resMap.put("rows", page.getResult());
			mapResult = resMap;
		} else {
			mapResult.clear();
		}
		return SUCCESS;
	}
	
	
	// 获取 单个 模板用户作品
	private Map<String, Object> userDocInfo =new HashMap<String, Object>();
	private String findDocId;// 文档ID
	private String findMobile;// 手机号
	private String findState;// 购买状态
	private String findDocUserId;// 用户ID查询
	private String findTestFlg;// 用户标记
	public String pageGetUserDocInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> parm =new HashMap<String, Object>();
		String pageNo = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		if(StringUtils.isNotEmpty(findDocId)){
			parm.put("doc_id",findDocId);
		}
		if(StringUtils.isNotEmpty(findDocUserId)){
			parm.put("user_id",findDocUserId);
		}
		if(StringUtils.isNotBlank(findMobile)){
			parm.put("mobile",findMobile);
		}
		if(StringUtils.isNotBlank(findState)){// 购买状态
			parm.put("state",findState);
		}
		if(StringUtils.isNotBlank(findTestFlg)){// 购买状态
			parm.put("test_flg",findTestFlg);
		}
		String topType = request.getParameter("topType");
		String designId = request.getParameter("disId");
		Map<String, Object> result =new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(topType) && StringUtils.isNotEmpty(designId)){ // 如果顶级分类不为空
			parm.put("design_id",designId);
			Page  page = null;
			if (Integer.parseInt(topType) == 1020){// 判断 如果是视频
				page = yjxUserDocStatsService.pageUserViderDoc(parm, Integer.parseInt(pageNo), Integer.parseInt(pageSize));
			} else {
				page = yjxUserDocStatsService.pageUserDesignDoc(parm, Integer.parseInt(pageNo), Integer.parseInt(pageSize));
			}
			result.put("total", page.getTotalCount());
			result.put("rows",page.getResult());
		}
		userDocInfo = result;
		return SUCCESS;
	}
	
	// 获取提纲分类
	public String getTypeForCombo() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String parent = request.getParameter("parent");
		String flg = request.getParameter("blkFlg");
		String topIds = request.getParameter("topIds");// 逗号连接顶级分类
		Map<String, Object> param = new HashMap<String, Object>();
			if(StringUtils.isNotBlank(parent)){
			param.put("parent", parent);
		}
			if(StringUtils.isNotBlank(topIds)){
			param.put("topIds", topIds);
		}
			if(StringUtils.isNotBlank(flg)){
			param.put("flg", flg);
		}
		typeComboList = yjxUserDocStatsService.getTypeForCombo(param);
			if(typeComboList != null && typeComboList.size()>0){
			// 添加空白项目
			    if("1".equals(flg)){
				TypeInfoDto blankItem = new TypeInfoDto();
				blankItem.setType_id(0L);
				blankItem.setType_name("[不限]");
				typeComboList.add(0, blankItem);
			}
			// 默认选中第一个
			typeComboList.get(0).setSelected(true);
		}
		return SUCCESS;
	}
	
	/*
	 * 修改应用次数
	 */
	public void editApplyCnt() throws Exception{
		HttpServletRequest request=ServletActionContext.getRequest();
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");//获取管理员信息
		if(user!=null&&user.getUser_id()!=null){
			String stats_id=request.getParameter("id");
			String applyCnt=request.getParameter("applyCnt");//应用次数
			if(stats_id!=null&&stats_id.length()>0){
				Map<String, Object> parm=new HashMap<String, Object>();
				parm.put("id", Long.valueOf(stats_id));
				parm.put("show_apply_cnt", Long.valueOf(applyCnt));
				parm.put("update_by", user.getUser_id());
				boolean b=yjxUserDocStatsService.editShowViewCnt(parm);
				if(b){
					this.writeJSON("ok");
				}else{
					this.writeJSON("no");
				}
			}else{
				this.writeJSON("no");
			}
		}else{
			this.writeJSON("no");
		}
	}
	
	// 批量更新应用次数
	public void batchSetTmplApplyCnt() throws Exception{
		HttpServletRequest request=ServletActionContext.getRequest();
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");//获取管理员信息
		if(user!=null&&user.getUser_id()!=null){
			String top_type =request.getParameter("top_type");
			String parent =request.getParameter("parent");
			String type_id = request.getParameter("type_id");
			String base=request.getParameter("base");
			String mult=request.getParameter("mult");
			if(top_type!=null && top_type.length()>0 && mult!=null&&mult.length()>0){
				Map<String, Object> parm=new HashMap<String, Object>();
				parm.put("update_by", user.getUser_id());
				parm.put("base", Long.valueOf(base));
				parm.put("mult", Long.valueOf(mult));
				parm.put("top_parent", Long.valueOf(top_type));
				parm.put("parent", Long.valueOf(parent));
				parm.put("type_id", Long.valueOf(type_id));
				boolean b=yjxUserDocStatsService.batchSetShowApplyCnt(parm);
				if(b){
					this.writeJSON("ok");
				}else{
					this.writeJSON("no");
				}
			}else{
				this.writeJSON("no");
			}
		}else{
			this.writeJSON("no");
		}
	}
	
	public HashMap<String, Object> getMapResult() {
		return mapResult;
	}
	
	public void setMapResult(HashMap<String, Object> mapResult) {
		this.mapResult = mapResult;
	}
	
	public String getIsCount() {
		return isCount;
	}
	
	public void setIsCount(String isCount) {
		this.isCount = isCount;
	}
	
	public String getFindDocName() {
		return findDocName;
	}
	
	public void setFindDocName(String findDocName) {
		this.findDocName = findDocName;
	}
	
	public String getFindTmplType() {
		return findTmplType;
	}
	
	public void setFindTmplType(String findTmplType) {
		this.findTmplType = findTmplType;
	}
	
	public String getFindDateStart() {
		return findDateStart;
	}
	
	public String getFindDateEnd() {
		return findDateEnd;
	}
	
	public void setFindDateStart(String findDateStart) {
		this.findDateStart = findDateStart;
	}
	
	public void setFindDateEnd(String findDateEnd) {
		this.findDateEnd = findDateEnd;
	}
	public List<TypeInfoDto> getTypeComboList() {
		return typeComboList;
	}
	public void setTypeComboList(List<TypeInfoDto> typeComboList) {
		this.typeComboList = typeComboList;
	}
	public String getFindTmplTypetop() {
		return findTmplTypetop;
	}
	public void setFindTmplTypetop(String findTmplTypetop) {
		this.findTmplTypetop = findTmplTypetop;
	}

	public String getFindPubFlg() {
		return findPubFlg;
	}

	public void setFindPubFlg(String findPubFlg) {
		this.findPubFlg = findPubFlg;
	}

	public HashMap<String, Object> getUserDocMapResult() {
		return userDocMapResult;
	}

	public void setUserDocMapResult(HashMap<String, Object> userDocMapResult) {
		this.userDocMapResult = userDocMapResult;
	}

	public UserDocInfoDto getAllUserDocInfo() {
		return allUserDocInfo;
	}

	public void setAllUserDocInfo(UserDocInfoDto allUserDocInfo) {
		this.allUserDocInfo = allUserDocInfo;
	}

	public YjxUserDocStatsService getYjxUserDocStatsService() {
		return yjxUserDocStatsService;
	}
	public void setYjxUserDocStatsService(
			YjxUserDocStatsService yjxUserDocStatsService) {
		this.yjxUserDocStatsService = yjxUserDocStatsService;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getFindUniTmplId() {
		return findUniTmplId;
	}

	public void setFindUniTmplId(String findUniTmplId) {
		this.findUniTmplId = findUniTmplId;
	}

	public String getFindUserId() {
		return findUserId;
	}

	public void setFindUserId(String findUserId) {
		this.findUserId = findUserId;
	}
	public String getFindState() {
		return findState;
	}
	public void setFindState(String findState) {
		this.findState = findState;
	}
	public String getFindDocId() {
		return findDocId;
	}
	public void setFindDocId(String findDocId) {
		this.findDocId = findDocId;
	}
	public String getFindMobile() {
		return findMobile;
	}
	public void setFindMobile(String findMobile) {
		this.findMobile = findMobile;
	}
	public Map<String, Object> getUserDocInfo() {
		return userDocInfo;
	}
	public void setUserDocInfo(Map<String, Object> userDocInfo) {
		this.userDocInfo = userDocInfo;
	}
	public String getFindDocUserId() {
		return findDocUserId;
	}
	public void setFindDocUserId(String findDocUserId) {
		this.findDocUserId = findDocUserId;
	}
	public String getFindTestFlg() {
		return findTestFlg;
	}
	public void setFindTestFlg(String findTestFlg) {
		this.findTestFlg = findTestFlg;
	}
	
}
